/**
 * List of [tblName] [tblTitle] .
 * Created by buzhizhe CodeConvert on 2020/3/6 11:58:29.
 */
$(function () {
    initPage();
    loadData();
});

//初始化页面
function initPage() {
    VM = new Vue({
        delimiters: ["[[", "]]"],
        el: '#app',
        data: {
            formdata: {
                username: "",
                pageindex: 1,
                pagesize: 20,
            },
            result: {
                total: 0,
                rows:[]
            },
        }
    });
}

//加载数据
function loadData() {
    getListData();
}

//获取列表数据
function getListData(pageindex) {
    if (pageindex != undefined) {
        VM.formdata.pageindex = pageindex;
    }
    var url = ApiHost + "/{subFolder}/[tblName]list";
    var formData = VM.formdata;
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        data: formData,
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            VM.result = result.data;
            dealData();
        }
    });
}

//处理数据
function dealData() {
    // fields: {strNames}
    pagerByVM(VM);
}

//打开编辑窗口
function popupEdit(id) {
    var url = PageHost + "/{subFolder}/[tblName]edit/?id=" + id;
    openLayer(url, "修改");
}
//打开查看窗口
function popupView(id) {
    var url = PageHost + "/{subFolder}/[tblName]view/?id=" + id;
    openLayer(url, "查看详情");
}

//删除记录
function popupDelete(id) {
    
    if (!layerConfirm("确定要删除此记录吗？删除后不可恢复！")) {
        return;
    }
    var url = ApiHost + "/{subFolder}/[tblName]delete";
    $.ajax({
        type: "post",
        url: url,
        data: {
            id: id
        },
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                //layerAlert("删除成功");
                layerCallBack();
            } else {
                layerAlert("提交失败：" + result.message);
            }
        }
    }
    );
}

//回调函数
function layerCallBack() {
    setTimeout('loadData()', 200);

}

